package config

import (
	"context"
	"fileNest/global"
	"fmt"
	"time"

	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
)

func InitMinio() {
	minioClient, err := minio.New(AppConfig.Minio.Endpoint, &minio.Options{
		Creds:  credentials.NewStaticV4(AppConfig.Minio.AccessKey, AppConfig.Minio.SecretKey, ""),
		Secure: AppConfig.Minio.UseSSL,
	})
	if err != nil {
		fmt.Errorf("Error initializing MinIO client: %v\n", err)
		return
	}
	global.Minio = minioClient
}

// 生成临时浏览url
func GeneratePresignedURL(bucketName, storagePath string) (presignedURL string, err error) {
	expires := time.Duration(10) * time.Minute
	url, err := global.Minio.PresignedGetObject(context.Background(), bucketName, storagePath, expires, nil)
	if err != nil {
		return
	}
	presignedURL = url.String()
	return
}
